# 记录了制作Linux发布包的所有过程 
# 编写Shell时注意判断文件是否已经下载，如果文件已经存在，则不重复下载
# 
# V0.1
# 此版本主要参考《深度探索Linux操作系统》一书
# 
# V0.2
# 参照Linux就是这个范进行更改

# 脚本遇到错误停止执行
set -e

# 编译过程中待下载软件包地址
# 读者可以使用更快的镜像进行下载
binutils_url="http://mirrors.cug.edu.cn/gnu/binutils/binutils-2.23.1.tar.bz2"
gcc_url="http://mirrors.cug.edu.cn/gnu/gcc/gcc-4.7.3/gcc-4.7.3.tar.bz2"
gmp_url="http://mirrors.cug.edu.cn/gnu/gmp/gmp-5.0.5.tar.bz2"
mpfr_url="http://mirrors.cug.edu.cn/gnu/mpfr/mpfr-3.1.1.tar.bz2"
mpc_url="http://mirrors.cug.edu.cn/gnu/mpc/mpc-1.0.1.tar.gz"

linux_url="https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.7.4.tar.bz2"

glibc_url="http://mirrors.cug.edu.cn/gnu/glibc/glibc-2.15.tar.xz"
#glibc_cupid_patch_url=TODO # 错误语句，补充下载链接地址
#glibc_frexp_patch_url=TODO # 同上

# 环境变量定义
unset LANG
export HOST=$MACHTYPE
export BUILD=$HOST
export TARGET=i686-none-linux-gnu
export CROSS_TOOL=/vita/cross-tool
export CROSS_GCC_TMP=/vita/cross-gcc-tmp
export SYSROOT=/vita/sysroot
PATH=$CROSS_TOOL/bin:$CROSS_GCC_TMP/bin:/sbin:/usr/sbin:$PATH

# GCC交叉编译工具的编译过程大体分四个阶段
# 1、 编译binutils,这些是一些二进制操作的工具
# 2、 编译独立的GCC，是一个不依赖C库的编译工具
# 3、 编译C库
# 4、 编译目标平台下的GCC

# 准备工作建立目录 
cd /
if [ ! -d "vita" ]; then
mkdir vita
fi
cd vita
if [ ! -d "source" ]; then
mkdir source
fi
if [ ! -d "build" ]; then
mkdir build
fi
if [ ! -d "cross-tool" ]; then
mkdir cross-tool
fi
if [ ! -d "cross-gcc-tmp" ]; then
mkdir cross-gcc-tmp
fi
if [ ! -d "sysroot" ]; then
mkdir sysroot
fi

# ---------------------------编译binutils  Start-------------------------------------------------------
echo "编译binutils,输入(y/n)......"
read answer
# 
#    判断源码文件是否下载，并下载文件
if [ $answer = "y" ]; then
cd /vita/source
if [ ! -f "binutils-2.23.1.tar.bz2" ]; then
wget $binutils_url
fi 
#    编译
cd /vita/build
tar xvf ../source/binutils-2.23.1.tar.bz2
if [ ! -d "binutils-build" ]; then
mkdir binutils-build
fi
cd binutils-build
#../binutils-2.23.1/configure --prefix=$CROSS_TOOL --disable-nls --disable-werror --target=$TARGET --with-sysroot=$SYSROOT
../binutils-2.23.1/configure --prefix=$CROSS_TOOL --target=$TARGET --with-sysroot=$SYSROOT
make 
make install
fi
# ---------------------------编译binutils  End-------------------------------------------------------


# ---------------------------编译独立GCC  Start-------------------------------------------------------
echo "编译独立GCC,输入(y/n)......"
read answer

if [ $answer = "y" ]; then
#    判断源码文件是否下载，并下载文件
cd /vita/source
if [ ! -f "gcc-4.7.3.tar.bz2" ]; then
wget $gcc_url
fi 
if [ ! -f "gmp-5.0.5.tar.bz2" ]; then
wget $gmp_url
fi
if [ ! -f "mpfr-3.1.1.tar.bz2" ]; then
wget $mpfr_url
fi
if [ ! -f "mpc-1.0.1.tar.gz" ]; then
wget $mpc_url
fi
#    编译
cd /vita/build
tar xvf ../source/gcc-4.7.3.tar.bz2
cd gcc-4.7.3

if [ -d "gmp" ]; then
rm -rf gmp
fi
if [ -d "mpfr" ]; then
rm -rf mpfr
fi
if [ -d "mpc" ]; then
rm -rf mpc
fi

tar xvf ../../source/gmp-5.0.5.tar.bz2
mv gmp-5.0.5/ gmp
tar xvf ../../source/mpfr-3.1.1.tar.bz2
mv mpfr-3.1.1/ mpfr
tar xvf ../../source/mpc-1.0.1.tar.gz
mv mpc-1.0.1/ mpc
cd /vita/build
if [ ! -d "gcc-build" ]; then
mkdir gcc-build
fi
cd gcc-build
#../gcc-4.7.3/configure \
#  --prefix=$CROSS_GCC_TMP --target=$TARGET \
#  --with-sysroot=$SYSROOT  \
#  --enable-languages=c \
#  --without-cloog  \
#  --with-mpfr-include=/vita/build/gcc-4.7.3/mpfr/src  \
#  --with-mpfr-lib=/vita/build/gcc-build/mpfr/src/.libs  \
#  --disable-shared --disable-multilib \
#  --disable-threads --disable-decimal-float  \
#  --disable-libquadmath --disable-libmudflap  \
#  --disable-libgomp --disable-target-libiberty --disable-target-zlib \
#  --disable-nls  \
#  --disable-libssp
../gcc-4.7.3/configure \
  --prefix=$CROSS_GCC_TMP --target=$TARGET \
  --with-sysroot=$SYSROOT  \
  --with-newlib  \
  --enable-languages=c \
  --with-mpfr-include=/vita/build/gcc-4.7.3/mpfr/src  \
  --with-mpfr-lib=/vita/build/gcc-build/mpfr/src/.libs  \
  --disable-shared  \
  --disable-threads --disable-decimal-float  \
  --disable-libquadmath --disable-libmudflap  \
  --disable-libgomp  \
  --disable-nls  \
  --disable-libssp
make
make install
cd /vita/cross-gcc-tmp
ln -s libgcc.a lib/gcc/i686-none-linux-gnu/4.7.3/libgcc_eh.a
fi
# ---------------------------编译独立GCC  End-------------------------------------------------------

# ---------------------------安装Linux头文件  Start-------------------------------------------------------
echo "安装Linux头文件,输入(y/n)......"
read answer

if [ $answer = "y" ]; then
#  判断Linux源码是否下载
cd /vita/source
if [ ! -f "linux-3.7.4.tar.bz2" ]; then
wget $linux_url
fi
#  安装头文件
cd /vita/build
tar xvf ../source/linux-3.7.4.tar.bz2
cd linux-3.7.4
make mrproper
#make ARCH=i386 headers_check
#make ARCH=i386 INSTALL_HDR_PATH=$SYSROOT/usr/ headers_install
make headers_check
make INSTALL_HDR_PATH=$SYSROOT/usr/ headers_install
fi
# ---------------------------安装Linux头文件  End-------------------------------------------------------

# ---------------------------编译 C库  Start-------------------------------------------------------
echo "编译 C库,输入(y/n)......"
read answer

if [ $answer = "y" ]; then
#  判断Linux源码是否下载
cd /vita/source
if [ ! -f "glibc-2.15.tar.xz" ]; then
wget $glibc_url
fi
#if [ ! -f "glic-2.15-cpuid.patch" ]; then
#wget $glibc_cupid_patch_url
#fi
#f [ ! -f "glibc-2.15-s_frexp.patch" ]; then
#wget $glibc_frexp_patch_url
#fi
#  安装头文件
cd /vita/build
tar xvf ../source/glibc-2.15.tar.xz
#cd glibc-2.15
#patch -p1 < ../../source/glibc-2.15-cpuid.patch
#patch -p1 < ../../source/glibc-2.15-s_frexp.patch
#cd ..
if [ ! -d "glibc-build" ]; then
mkdir glibc-build
fi
cd glibc-build
#../glibc-2.15/configure \
#  --prefix=/usr --host=$TARGET  \
#  --build=$(../glibc-2.15/scripts/config.guess)  \
#  --disable-profile  \
#  --enable-kernel=3.7.4 --enable-add-ons  \
#  --with-headers=$SYSROOT/usr/include  \
#  libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes

# 覆盖修改后的configure文件，修正cpuid.h文件不能找到问题 
cp /vita/glibc_patch/configure /vita/build/glibc-2.15/sysdeps/i386/configure

../glibc-2.15/configure \
  --prefix=/usr --host=$TARGET  \
  --enable-kernel=3.7.4 --enable-add-ons  \
  --with-headers=$SYSROOT/usr/include  \
  libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes  \
  libc_cv_ctors_header=yes
make
make install_root=$SYSROOT install
fi
# ---------------------------编译 C库  End-------------------------------------------------------

# ---------------------------第二次binutils编译  Start-------------------------------------------------------
echo "第二次binutils编译,输入(y/n)......"
read answer

if [ $answer = "y" ]; then
cd /vita/build/binutils-build
CC="i686-none-linux-gnu-gcc -B/vita/cross-tool/bin" \
AR=i686-none-linux-gnu-ar RANLIB=i686-none-linux-gnu-ranlib \
../binutils-2.23.1/configure --prefix=$CROSS_TOOL --disable-nls  \
--with-lib-path=$CROSS_TOOL/lib64
make 
make install
make -C ld clean
make -C ld LIB_PATH=/usr/lib64:/lib64
cp -v ld/ld-new $CROSS_TOOL/bin
fi
# ---------------------------第二次binutils编译  End-------------------------------------------------------

# ---------------------------编译目标平台GCC  Start-------------------------------------------------------
echo "编译目标平台GCC,输入(y/n)......"
read answer

if [ $answer = "y" ]; then
cd /vita/gcc-build
CC="$i686-none-linux-gnu-gcc -B/vita/cross-tool/bin"  \
AR=$i686-none-linux-gnu-ar RANLIB=$i686-none-linux-gnu-ranlib  \
../gcc-4.7.c/configure  \
  --prefix=$CROSS_TOOL --with-local-prefix=$CROSS_TOOL --enable-clocale=gnu  \
  --enable-shared --enable-__cxa_atexit --disable-libstdcxx-pch --disable-multlib  \
  --disable-bootstrap --disable-libgomp  \
  --without-ppl --without-cloog  \
  --target=$TARGET  \
  --with-sysroot=$SYSROOT  \
  --with-mpfr-include=/vita/build/gcc-4.7.3/mpfr/src  \
  --with-mpfr-lib=/vita/build/gcc-build/mpfr/src/.libs  \
  --enable-languages=c,c++ --enable-threads=posix
make
make install
fi
# ---------------------------编译目标平台GCC  End-------------------------------------------------------

